package com.flowerSystem.controller;

import com.flowerSystem.entity.Producttype;
import com.flowerSystem.service.ProductTypeService;
import com.flowerSystem.util.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@CrossOrigin
@RestController
@RequestMapping("/manager")
public class ProductTypeController {
    @Resource
    private ProductTypeService productTypeService;

    /**
     * 产品分类表（添加类别）
     * @param productType
     * @return
     */
    @PostMapping("/productType")
    public ResponseResult productTypeAdd(@RequestBody Producttype productType){
        if(productType != null){
            boolean save = productTypeService.save(productType);
            if(save){
                return ResponseResult.ok(productType);
            }
        }
        return ResponseResult.fail();
    }

    /**
     * 产品分类表(通过id删除指定类别)
     * @param id
     * @return
     */
    @DeleteMapping("/productType/{id}")
    public ResponseResult productTypeDel(@PathVariable("id") int id){
        boolean b = productTypeService.removeById(id);
        if(b){
            return ResponseResult.ok(null);
        }
        return ResponseResult.fail();
    }

    /**
     * 产品分类(获取所有的产品类别)
     * @return
     */
    @GetMapping("/productType")
    public ResponseResult productTypeSelectAll(){
        List<Producttype> list = productTypeService.getProductTypeAll();
        if(list != null && !list.isEmpty() && list.size() > 0){
            return ResponseResult.ok(list);
        }
        return ResponseResult.fail();
    }
}
